<?php
class MallModel extends BaseModel
{
    public $_connect = null;

    public function _initialize()
    {
        if ($this->_connect == null) {
            $model = new Model();
            $this->_connect = $model->db(2, 'mysql://root:@localhost/mall');
        }
    }

    /**
     * 析构方法
     */
    public function __destruct()
    {
        unset($this->_connect);
    }

    /**
     * 查询多个
     * @param string $table
     * @param string $field
     * @param array $map
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function query($table, $field = null, $map = null, $order = null, $limit = null)
    {
        $this->_connect->table($table); //设置操作的数据表
        $list = $this->_connect->where($map)->field($field)->order($order)->limit($limit)->select(); //查询
        //dump($this->_connect->getLastSql());
        return $list;
    }

    /**
     * 查询一个
     * @param string $table
     * @param string $field
     * @param array $map
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function findOne($table, $field = null, $map = null)
    {
        $this->_connect->table($table); //设置操作的数据表
        $list = $this->_connect->where($map)->field($field)->find(); //查询
        return $list;
    }

    /**
     * 获取商场列表
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getList()
    {
        return $this->query('m_market', 'id,name');
    }

    /**
     * 获取一个商场信息
     * @param int $mid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getMall($mid)
    {
        $map['id'] = $mid;
        return $this->findOne('m_market', 'id,name', $map);
    }

    /**
     * 获取商场楼层列表
     * @param int $mid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getFloorList($mid, $field = 'id,mid,concat(sain,name) as name')
    {
        $map = array();
        $map['mid'] = $mid;
        return $this->query('m_floor', $field, $map, '\'order\' ASC');
    }

    /**
     * 获取商场楼层列表
     * @param int $mid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getFloorListByWhere($map, $field = 'id,mid,concat(sain,name) as name')
    {
        return $this->query('m_floor', $field, $map, '\'order\' ASC');
    }

    /**
     * 获取一个商场信息
     * @param int $mid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getFloor($fid)
    {
        $map['id'] = $fid;
        return $this->findOne('m_floor', 'id,mid,concat(sain,name) as name', $map);
    }

    /**
     * 获取商场楼层品牌列表
     * @param int $fid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getFloorBrandList($fid, $where = null)
    {
        $map['fid'] = $fid;
        if ($where) {
            $map = array_merge($map, $where);
        }
        $brands = $this->query('m_floor_brand', 'bid,mid', $map);
        //获取对应品牌信息
        unset($map);
        foreach ($brands as $k => $v) {
            $map['id'] = $v['bid'];
            $brand = $this->findOne('m_brand', 'id,name', $map);
            if ($brand) {
                $brand['mid'] = $v['mid'];
                $brands[$k] = $brand;
            } else {
                unset($brands[$k]);
            }
        }
        return $brands;
    }

    /**
     * 获取品牌信息
     * @param int $bid
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrand($bid)
    {
        $map['id'] = $bid;
        return $this->findOne('m_brand', 'id,name', $map);
    }

    /**
     * 获取品牌列表
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function getBrandList($field = 'id,name,ename,initials', $map = null, $limit = null)
    {
        return $this->query('m_brand', $field, $map, $limit);
    }

    public function setAddress($task)
    {
        if ($task['mid']) {
            $task['mall'] = $this->getMall($task['mid']);
        }
        if ($task['fid']) {
            $task['floor'] = $this->getFloor($task['fid']);
        }
        if ($task['bid']) {
            $task['brand'] = $this->getBrand($task['bid']);
        }
        return $task;
    }
}